<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="昵称" prop="nickname">
        <el-input
          v-model="queryParams.nickname"
          placeholder="请输入昵称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="真实姓名" prop="realName">
        <el-input
          v-model="queryParams.realName"
          placeholder="请输入真实姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="phoneNumber">
        <el-input
          v-model="queryParams.phoneNumber"
          placeholder="请输入手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['owner:owner:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['owner:owner:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['owner:owner:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['owner:owner:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="ownerList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="人脸照片" align="center" prop="face" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.face" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="昵称" align="center" prop="nickname"/>
      <el-table-column label="真实姓名" align="center" prop="realName"/>
      <el-table-column label="性别" align="center" prop="gender"/>
      <el-table-column label="手机号" align="center" prop="phoneNumber"/>
      <el-table-column label="微信号" align="center" prop="wecharId"/>
      <el-table-column label="出生年月" align="center" prop="bornDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.bornDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="个性签名" align="center" prop="sign"/>
      <el-table-column label="状态" align="center" prop="status"/>
      <el-table-column label="排序" align="center" prop="sort"/>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['owner:owner:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['owner:owner:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改业主管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="580px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="昵称" prop="nickname">
          <el-input v-model="form.nickname" placeholder="请输入昵称"/>
        </el-form-item>
        <el-form-item label="真实姓名" prop="realName">
          <el-input v-model="form.realName" placeholder="请输入真实姓名"/>
        </el-form-item>
        <el-form-item label="性别" prop="realName">
          <el-radio-group v-model="form.gender">
            <el-radio label="男"/>
            <el-radio label="女"/>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="身份证" prop="idCard">
          <el-input v-model="form.idCard" placeholder="请输入身份证号码"/>
        </el-form-item>
        <el-form-item label="手机号" prop="phoneNumber">
          <el-input v-model="form.phoneNumber" placeholder="请输入手机号"/>
        </el-form-item>
        <el-form-item label="微信号" prop="wecharId">
          <el-input v-model="form.wecharId" placeholder="请输入微信号"/>
        </el-form-item>
        <el-form-item label="出生年月" prop="bornDate">
          <el-date-picker clearable
                          v-model="form.bornDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="由身份证号计算得出"
                          disabled>
          </el-date-picker>
        </el-form-item>
        <el-form-item label="人脸照片" prop="face">
          <div style="width: 460px">
            <div class="videoFace" v-model="video" src="" ref="video"></div>
            <el-button class="vfButton" type="primary">拍摄</el-button>
            <div class="videoFace" v-model="form.face"></div>
          </div>
        </el-form-item>
        <el-form-item label="头像" prop="avatar">
          <el-input v-model="form.avatar" placeholder="请输入头像"/>
        </el-form-item>
        <el-form-item label="个性签名" prop="sign">
          <el-input v-model="form.sign" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio label="0" :value="0">正常</el-radio>
            <el-radio label="1" :value="1">停用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listOwner, getOwner, delOwner, addOwner, updateOwner} from "@/api/owner/owner";

export default {
  name: "Owner",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 业主管理表格数据
      ownerList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        ownerId: null,
        nickname: null,
        realName: null,
        gender: null,
        idCard: null,
        phoneNumber: null,
        openid: null,
        wecharId: null,
        bornDate: null,
        avatar: null,
        sign: null,
        status: null,
        password: null,
        sort: null,
        face: null,
      },
      // 表单参数
      form: {
        status: 1,
      },
      // 表单校验
      rules: {
        realName: [
          {required: true, message: "真实姓名不能为空", trigger: "blur"}
        ],
        gender: [
          {required: true, message: "性别：0男1女不能为空", trigger: "change"}
        ],
        idCard: [
          {required: true, message: "身份证号码不能为空", trigger: "blur"}
        ],
        phoneNumber: [
          {required: true, message: "手机号不能为空", trigger: "blur"}
        ],
        status: [
          {required: true, message: "状态：0正常1禁用不能为空", trigger: "change"}
        ],
        password: [
          {required: true, message: "登陆密码不能为空", trigger: "blur"}
        ],
        sort: [
          {required: true, message: "排序不能为空", trigger: "blur"}
        ],
      },
      // 人脸
      video: undefined,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询业主管理列表 */
    getList() {
      this.loading = true;
      listOwner(this.queryParams).then(response => {
        this.ownerList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        ownerId: null,
        nickname: null,
        realName: null,
        gender: null,
        idCard: null,
        phoneNumber: null,
        openid: null,
        wecharId: null,
        bornDate: null,
        avatar: null,
        sign: null,
        status: null,
        password: null,
        sort: null,
        remark: null,
        delFlag: null,
        face: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
      this.initCamera();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.ownerId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加业主管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const ownerId = row.ownerId || this.ids
      getOwner(ownerId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改业主管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.ownerId != null) {
            updateOwner(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOwner(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ownerIds = row.ownerId || this.ids;
      this.$modal.confirm('是否确认删除业主管理编号为"' + ownerIds + '"的数据项？').then(function () {
        return delOwner(ownerIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('owner/owner/export', {
        ...this.queryParams
      }, `owner_${new Date().getTime()}.xlsx`)
    },
    initCamera(){
      // 获取视频流数据
      navigator.mediaDevices.getUserMedia({
        // 声音
        audio: false,
        // 视频
        video: true
      }).then(stream => {
        this.$refs['video'].srcObject = stream;
      })
    }
  }
};
</script>
<style>
.videoFace{
  display: inline-block;
  width: 160px;
  height: 160px;
  border: 1px solid darkgray;
}
.vfButton{
  margin: 10px;
  position: relative;
  bottom: 74px;
  left: 0px;
}
</style>
